﻿using System;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using Lib.CommonForm;

public class CustomReportViewerMessages : IReportViewerMessages
{
    #region MemberFields

    private string backButtonToolTip = null;
    private string backMenuItemText = null;
    private string changeCredentialsText = null;
    private string _currentPageTextBoxToolTip = null;
    private string documentMapButtonToolTip = null;
    private string documentMapMenuItemText = null;
    private string exportButtonToolTip = null;
    private string exportMenuItemText = null;
    private string falseValueText = null;
    private string findButtonText = null;
    private string findButtonToolTip = null;
    private string findNextButtonText = null;
    private string findNextButtonToolTip = null;
    private string firstPageButtonToolTip = null;
    private string lastPageButtonToolTip = null;
    private string nextPageButtonToolTip = null;
    private string noMoreMatches = null;
    private string nullCheckBoxText = null;
    private string nullCheckBoxToolTip = null;
    private string nullValueText = null;
    private string pageOf = null;
    private string pageSetupButtonToolTip = null;
    private string pageSetupMenuItemText = null;
    private string parameterAreaButtonToolTip = null;
    private string passwordPrompt = null;
    private string previousPageButtonToolTip = null;
    private string printButtonToolTip = null;
    private string printLayoutButtonToolTip = null;
    private string printLayoutMenuItemText = null;
    private string printMenuItemText = null;
    private string progressText = null;
    private string refreshButtonToolTip = null;
    private string refreshMenuItemText = null;
    private string searchTextBoxToolTip = null;
    private string selectAll = null;
    private string selectAValue = null;
    private string stopButtonToolTip = null;
    private string stopMenuItemText = null;
    private string textNotFound = null;
    private string totalPagesToolTip = null;
    private string trueValueText = null;
    private string userNamePrompt = null;
    private string viewReportButtonText = null;
    private string viewReportButtonToolTip = null;
    private string zoomControlToolTip = null;
    private string zoomMenuItemText = null;
    private string zoomToPageWidth = null;
    private string zoomToWholePage = null;

    #endregion

    #region Constructors

    public CustomReportViewerMessages(IReportViewerMessages reportViewerMessages)
    {
        this.InitializeMessages(reportViewerMessages);
    }

    #endregion

    #region Methods

    private void InitializeMessages(IReportViewerMessages reportViewerMessages)
    {
        if (reportViewerMessages != null)
        {
            this.backButtonToolTip = reportViewerMessages.BackButtonToolTip;
            this.backMenuItemText = reportViewerMessages.BackMenuItemText;
            this.changeCredentialsText = reportViewerMessages.ChangeCredentialsText;
            this._currentPageTextBoxToolTip = reportViewerMessages.CurrentPageTextBoxToolTip;
            this.documentMapButtonToolTip = reportViewerMessages.DocumentMapButtonToolTip;
            this.documentMapMenuItemText = reportViewerMessages.DocumentMapMenuItemText;
            this.exportButtonToolTip = reportViewerMessages.ExportButtonToolTip;
            this.exportMenuItemText = reportViewerMessages.ExportMenuItemText;
            this.falseValueText = reportViewerMessages.FalseValueText;
            this.findButtonText = reportViewerMessages.FindButtonText;
            this.findButtonToolTip = reportViewerMessages.FindButtonToolTip;
            this.findNextButtonText = reportViewerMessages.FindNextButtonText;
            this.findNextButtonToolTip = reportViewerMessages.FindNextButtonToolTip;
            this.firstPageButtonToolTip = reportViewerMessages.FirstPageButtonToolTip;
            this.lastPageButtonToolTip = reportViewerMessages.LastPageButtonToolTip;
            this.nextPageButtonToolTip = reportViewerMessages.NextPageButtonToolTip;
            this.noMoreMatches = reportViewerMessages.NoMoreMatches;
            this.nullCheckBoxText = reportViewerMessages.NullCheckBoxText;
            this.nullCheckBoxToolTip = reportViewerMessages.NullCheckBoxToolTip;
            this.nullValueText = reportViewerMessages.NullValueText;
            this.pageOf = reportViewerMessages.PageOf;
            this.pageSetupButtonToolTip = reportViewerMessages.PageSetupButtonToolTip;
            this.pageSetupMenuItemText = reportViewerMessages.PageSetupMenuItemText;
            this.parameterAreaButtonToolTip = reportViewerMessages.ParameterAreaButtonToolTip;
            this.passwordPrompt = reportViewerMessages.PasswordPrompt;
            this.previousPageButtonToolTip = reportViewerMessages.PreviousPageButtonToolTip;
            this.printButtonToolTip = reportViewerMessages.PrintButtonToolTip;
            this.printLayoutButtonToolTip = reportViewerMessages.PrintLayoutButtonToolTip;
            this.printLayoutMenuItemText = reportViewerMessages.PrintLayoutMenuItemText;
            this.printMenuItemText = reportViewerMessages.PrintMenuItemText;
            this.progressText = reportViewerMessages.ProgressText;
            this.refreshButtonToolTip = reportViewerMessages.RefreshButtonToolTip;
            this.refreshMenuItemText = reportViewerMessages.RefreshMenuItemText;
            this.searchTextBoxToolTip = reportViewerMessages.SearchTextBoxToolTip;
            this.selectAll = reportViewerMessages.SelectAll;
            this.selectAValue = reportViewerMessages.SelectAValue;
            this.stopButtonToolTip = reportViewerMessages.StopButtonToolTip;
            this.stopMenuItemText = reportViewerMessages.StopMenuItemText;
            this.textNotFound = reportViewerMessages.TextNotFound;
            this.totalPagesToolTip = reportViewerMessages.TotalPagesToolTip;
            this.trueValueText = reportViewerMessages.TrueValueText;
            this.userNamePrompt = reportViewerMessages.UserNamePrompt;
            this.viewReportButtonText = reportViewerMessages.ViewReportButtonText;
            this.viewReportButtonToolTip = reportViewerMessages.ViewReportButtonToolTip;
            this.zoomControlToolTip = reportViewerMessages.ZoomControlToolTip;
            this.zoomMenuItemText = reportViewerMessages.ZoomMenuItemText;
            this.zoomToPageWidth = reportViewerMessages.ZoomToPageWidth;
            this.zoomToWholePage = reportViewerMessages.ZoomToWholePage;
        }
    }

    #endregion

    #region Properties

    #region .IReportViewerMessages implementation

    public string BackButtonToolTip
    {
        get
        {
            return this.backButtonToolTip;
        }
        set
        {
            this.backButtonToolTip = value;
        }
    }

    public string BackMenuItemText
    {
        get
        {
            return this.backMenuItemText;
        }
        set
        {
            this.backMenuItemText = value;
        }
    }

    public string ChangeCredentialsText
    {
        get
        {
            return this.changeCredentialsText;
        }
        set
        {
            this.changeCredentialsText = value;
        }
    }

    public string CurrentPageTextBoxToolTip
    {
        get
        {
            return this._currentPageTextBoxToolTip;
        }
        set
        {
            this._currentPageTextBoxToolTip = value;
        }
    }

    public string DocumentMapButtonToolTip
    {
        get
        {
            return this.documentMapButtonToolTip;
        }
        set
        {
            this.documentMapButtonToolTip = value;
        }
    }

    public string DocumentMapMenuItemText
    {
        get
        {
            return this.documentMapMenuItemText;
        }
        set
        {
            this.documentMapMenuItemText = value;
        }
    }

    public string ExportButtonToolTip
    {
        get
        {
            return this.exportButtonToolTip;
        }
        set
        {
            this.exportButtonToolTip = value;
        }
    }

    public string ExportMenuItemText
    {
        get
        {
            return this.exportMenuItemText;
        }
        set
        {
            this.exportMenuItemText = value;
        }
    }

    public string FalseValueText
    {
        get
        {
            return this.falseValueText;
        }
        set
        {
            this.falseValueText = value;
        }
    }

    public string FindButtonText
    {
        get
        {
            return this.findButtonText;
        }
        set
        {
            this.findButtonText = value;
        }
    }

    public string FindButtonToolTip
    {
        get
        {
            return this.findButtonToolTip;
        }
        set
        {
            this.findButtonToolTip = value;
        }
    }

    public string FindNextButtonText
    {
        get
        {
            return this.findNextButtonText;
        }
        set
        {
            this.findNextButtonText = value;
        }
    }

    public string FindNextButtonToolTip
    {
        get
        {
            return this.findNextButtonToolTip;
        }
        set
        {
            this.findNextButtonToolTip = value;
        }
    }

    public string FirstPageButtonToolTip
    {
        get
        {
            return this.firstPageButtonToolTip;
        }
        set
        {
            this.firstPageButtonToolTip = value;
        }
    }

    public string LastPageButtonToolTip
    {
        get
        {
            return this.lastPageButtonToolTip;
        }
        set
        {
            this.lastPageButtonToolTip = value;
        }
    }

    public string NextPageButtonToolTip
    {
        get
        {
            return this.nextPageButtonToolTip;
        }
        set
        {
            this.nextPageButtonToolTip = value;
        }
    }

    public string NoMoreMatches
    {
        get
        {
            return this.noMoreMatches;
        }
        set
        {
            this.noMoreMatches = value;
        }
    }

    public string NullCheckBoxText
    {
        get
        {
            return this.nullCheckBoxText;
        }
        set
        {
            this.nullCheckBoxText = value;
        }
    }

    public string NullCheckBoxToolTip
    {
        get
        {
            return this.nullCheckBoxToolTip;
        }
        set
        {
            this.nullCheckBoxToolTip = value;
        }
    }

    public string NullValueText
    {
        get
        {
            return this.nullValueText;
        }
        set
        {
            this.nullValueText = value;
        }
    }

    public string PageOf
    {
        get
        {
            return this.pageOf;
        }
        set
        {
            this.pageOf = value;
        }
    }

    public string PageSetupButtonToolTip
    {
        get
        {
            return this.pageSetupButtonToolTip;
        }
        set
        {
            this.pageSetupButtonToolTip = value;
        }
    }

    public string PageSetupMenuItemText
    {
        get
        {
            return this.pageSetupMenuItemText;
        }
        set
        {
            this.pageSetupMenuItemText = value;
        }
    }

    public string ParameterAreaButtonToolTip
    {
        get
        {
            return this.parameterAreaButtonToolTip;
        }
        set
        {
            this.parameterAreaButtonToolTip = value;
        }
    }

    public string PasswordPrompt
    {
        get
        {
            return this.passwordPrompt;
        }
        set
        {
            this.passwordPrompt = value;
        }
    }

    public string PreviousPageButtonToolTip
    {
        get
        {
            return this.previousPageButtonToolTip;
        }
        set
        {
            this.previousPageButtonToolTip = value;
        }
    }

    public string PrintButtonToolTip
    {
        get
        {
            return this.printButtonToolTip;
        }
        set
        {
            this.printButtonToolTip = value;
        }
    }

    public string PrintLayoutButtonToolTip
    {
        get
        {
            return this.printLayoutButtonToolTip;
        }
        set
        {
            this.printLayoutButtonToolTip = value;
        }
    }

    public string PrintLayoutMenuItemText
    {
        get
        {
            return this.printLayoutMenuItemText;
        }
        set
        {
            this.printLayoutMenuItemText = value;
        }
    }

    public string PrintMenuItemText
    {
        get
        {
            return this.printMenuItemText;
        }
        set
        {
            this.printMenuItemText = value;
        }
    }

    public string ProgressText
    {
        get
        {
            return this.progressText;
        }
        set
        {
            this.progressText = value;
        }
    }

    public string RefreshButtonToolTip
    {
        get
        {
            return this.refreshButtonToolTip;
        }
        set
        {
            this.refreshButtonToolTip = value;
        }
    }

    public string RefreshMenuItemText
    {
        get
        {
            return this.refreshMenuItemText;
        }
        set
        {
            this.refreshMenuItemText = value;
        }
    }

    public string SearchTextBoxToolTip
    {
        get
        {
            return this.searchTextBoxToolTip;
        }
        set
        {
            this.searchTextBoxToolTip = value;
        }
    }

    public string SelectAll
    {
        get
        {
            return this.selectAll;
        }
        set
        {
            this.selectAll = value;
        }
    }

    public string SelectAValue
    {
        get
        {
            return this.selectAValue;
        }
        set
        {
            this.selectAValue = value;
        }
    }

    public string StopButtonToolTip
    {
        get
        {
            return this.stopButtonToolTip;
        }
        set
        {
            this.stopButtonToolTip = value;
        }
    }

    public string StopMenuItemText
    {
        get
        {
            return this.stopMenuItemText;
        }
        set
        {
            this.stopMenuItemText = value;
        }
    }

    public string TextNotFound
    {
        get
        {
            return this.textNotFound;
        }
        set
        {
            this.textNotFound = value;
        }
    }

    public string TotalPagesToolTip
    {
        get
        {
            return this.totalPagesToolTip;
        }
        set
        {
            this.totalPagesToolTip = value;
        }
    }

    public string TrueValueText
    {
        get
        {
            return this.trueValueText;
        }
        set
        {
            this.trueValueText = value;
        }
    }

    public string UserNamePrompt
    {
        get
        {
            return this.userNamePrompt;
        }
        set
        {
            this.userNamePrompt = value;
        }
    }

    public string ViewReportButtonText
    {
        get
        {
            return this.viewReportButtonText;
        }
        set
        {
            this.viewReportButtonText = value;
        }
    }

    public string ViewReportButtonToolTip
    {
        get
        {
            return this.viewReportButtonToolTip;
        }
        set
        {
            this.viewReportButtonToolTip = value;
        }
    }

    public string ZoomControlToolTip
    {
        get
        {
            return this.zoomControlToolTip;
        }
        set
        {
            this.zoomControlToolTip = value;
        }
    }

    public string ZoomMenuItemText
    {
        get
        {
            return this.zoomMenuItemText;
        }
        set
        {
            this.zoomMenuItemText = value;
        }
    }

    public string ZoomToPageWidth
    {
        get
        {
            return this.zoomToPageWidth;
        }
        set
        {
            this.zoomToPageWidth = value;
        }
    }

    public string ZoomToWholePage
    {
        get
        {
            return this.zoomToWholePage;
        }
        set
        {
            this.zoomToWholePage = value;
        }
    }

    #endregion

    #endregion
}

public class CustomizeMessages
{
    public CustomizeMessages(ReportViewer report)
    {
        report.ShowProgress = false;
        CustomReportViewerMessages customMessages = new CustomReportViewerMessages(report.Messages);
        customMessages.FindButtonText = "Tìm kiếm";
        customMessages.FindButtonToolTip = "Tìm kiếm";
        customMessages.FindNextButtonText = "Tìm kiếm thêm";
        customMessages.FindNextButtonToolTip = "Tìm kiếm thêm";
        customMessages.SearchTextBoxToolTip = @"Gõ từ khoá vào đây";
        customMessages.TextNotFound = @"Không tìm thấy";
        customMessages.NoMoreMatches = @"Không tìm thấy kết quả";
        customMessages.BackButtonToolTip = @"Trở lại";
        customMessages.BackMenuItemText = @"Trở lại";
        customMessages.DocumentMapButtonToolTip = @"DocumentMap";
        customMessages.DocumentMapMenuItemText = @"DocumentMap";
        customMessages.ExportButtonToolTip = @"Xuất dữ liệu";
        customMessages.ExportMenuItemText = @"Xuất dữ liệu";
        customMessages.FalseValueText = @"Sai";
        customMessages.FirstPageButtonToolTip = @"Trang đầu";
        customMessages.LastPageButtonToolTip = @"Trang cuối";
        customMessages.NextPageButtonToolTip = @"Trang tiếp theo";
        customMessages.PageOf = @"/";
        customMessages.PageSetupButtonToolTip = @"Đặt khổ giấy";
        customMessages.PageSetupMenuItemText = @"Đặt khổ giấy";
        customMessages.ParameterAreaButtonToolTip = @"Parameter";
        customMessages.PasswordPrompt = @"Password";
        customMessages.PreviousPageButtonToolTip = @"Trang trước";
        customMessages.PrintButtonToolTip = @"In báo cáo";
        customMessages.PrintLayoutButtonToolTip = @"Layout";
        customMessages.PrintLayoutMenuItemText = @"Layout";
        customMessages.PrintMenuItemText = @"In báo cáo";
        customMessages.ProgressText = @"Đang nạp dữ liệu";
        customMessages.RefreshButtonToolTip = @"Nạp lại báo cáo";
        customMessages.RefreshMenuItemText = @"Nạp lại báo cáo";
        customMessages.SelectAll = @"Select All";
        customMessages.SelectAValue = @"Select";
        customMessages.StopButtonToolTip = @"Dừng lại";
        customMessages.StopMenuItemText = @"Dừng lại";
        customMessages.TotalPagesToolTip = @"Tổng số trang";
        customMessages.TrueValueText = @"Đúng";
        customMessages.UserNamePrompt = @"UserName";
        customMessages.ViewReportButtonText = @"Xem báo cáo";
        customMessages.ViewReportButtonToolTip = @"Xem báo cáo";
        customMessages.ZoomControlToolTip = @"Zoom";
        customMessages.ZoomMenuItemText = @"Zoom";
        customMessages.ZoomToPageWidth = @"Xem theo độ rộng trang";
        customMessages.ZoomToWholePage = @"Xem toàn bộ trang";

        customizeReportViewer(report);

        report.Messages = customMessages;
    }

    ImageList reportViewerImageList;

    ReportViewer reportViewer1;

    void customizeReportViewer(ReportViewer report)
    {
        reportViewer1 = report;

        ToolStrip toolStrip = ToolStrip;
        if (toolStrip != null)
        {

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ImageForReportViewer));
            reportViewerImageList = new System.Windows.Forms.ImageList();
            reportViewerImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("reportViewerImageList.ImageStream")));
            reportViewerImageList.TransparentColor = System.Drawing.Color.Magenta;
            reportViewerImageList.Images.SetKeyName(0, "btnClose.Image");
            reportViewerImageList.Images.SetKeyName(1, "btnHelp.Image");
            reportViewerImageList.Images.SetKeyName(2, "btnFind.Image");
            reportViewerImageList.Images.SetKeyName(3, "btnFindNext.Image");
            reportViewerImageList.Images.SetKeyName(4, "btnExcel.Image");
            reportViewerImageList.Images.SetKeyName(5, "btnPdf.Image");

            toolStrip.ImageList = reportViewerImageList;

            toolStrip.Items["find"].ImageKey = "btnFind.Image";
            toolStrip.Items["findNext"].ImageKey = "btnFindNext.Image";

            int zoomPos = toolStrip.Items.IndexOfKey("zoom");
            if (zoomPos >= 0)
            {
                toolStrip.Items.Insert(zoomPos + 1, addDefaultSeparator("additionalSeparator1"));
                toolStrip.Items.Insert(zoomPos + 1, addDefaultLabel("lblSeparator", String.Empty, 100));
                toolStrip.Items.Insert(zoomPos + 1, addDefaultSeparator("additionalSeparator2"));
                toolStrip.Items.Insert(zoomPos + 1, addDefaultButton("btnClose", "Đóng", "btnClose.Image", new EventHandler(ButtonClose)));
                toolStrip.Items.Insert(zoomPos + 1, addDefaultButton("btnHelp", "Trợ giúp", "btnHelp.Image", null));
                toolStrip.Items.Insert(zoomPos + 1, addDefaultSeparator("additionalSeparator3"));
            }

            ToolStripDropDownButton exportButton = toolStrip.Items["export"] as ToolStripDropDownButton;
            if (exportButton != null)
                exportButton.DropDownOpened += new EventHandler(OnExportOpened);
        }
    }

    ToolStripButton addDefaultButton(string name, string text, string imageResource, EventHandler eventHandler)
    {
        ToolStripButton result = new ToolStripButton();

        result.Name = name;
        result.Size = new System.Drawing.Size(23, 22);
        result.Text = text;

        if (imageResource == String.Empty)
            result.DisplayStyle = ToolStripItemDisplayStyle.Text;
        else
        {
            result.DisplayStyle = ToolStripItemDisplayStyle.Image;
            result.ImageKey = imageResource;
        }

        if (eventHandler != null)
            result.Click += eventHandler;

        return result;
    }

    ToolStripLabel addDefaultLabel(string name, string text)
    {
        return addDefaultLabel(name, text, 0);
    }

    ToolStripLabel addDefaultLabel(string name, string text, int width)
    {
        ToolStripLabel result = new ToolStripLabel();

        result.Name = name;
        result.Text = text;

        result.AutoSize = (width <= 0);

        if (!result.AutoSize)
            result.Size = new System.Drawing.Size(width, 22);

        return result;
    }

    ToolStripSeparator addDefaultSeparator(string name)
    {
        ToolStripSeparator result = new ToolStripSeparator();

        result.Name = name;
        result.Size = new System.Drawing.Size(6, 25);

        return result;
    }

    TClass FindToolStrip<TClass>(Control control)
      where TClass : System.Windows.Forms.Control
    {
        if (control == null)
            return default(TClass);
        else if (control is TClass)
            return (TClass)control;
        else
        {
            TClass result = default(TClass);
            foreach (Control embedded in control.Controls)
            {
                if (result == null)
                    result = FindToolStrip<TClass>(embedded);
            }

            return result;
        }
    }

    void ButtonClose(object sender, EventArgs e)
    {
        if (reportViewer1.ParentForm != null)
            reportViewer1.ParentForm.Close();
    }

    void OnExportOpened(object sender, EventArgs e)
    {
        if (sender is ToolStripDropDownButton)
        {
            ToolStripDropDownButton button = (ToolStripDropDownButton)sender;

            foreach (ToolStripItem item in button.DropDownItems)
            {
                RenderingExtension extension = (RenderingExtension)item.Tag;
                if (extension != null)
                {
                    switch (extension.Name)
                    {
                        case "Word":
                            item.Image = this.reportViewerImageList.Images["btnWord.jpg"];
                            break;
                        case "Excel":
                            item.Image = reportViewerImageList.Images["btnExcel.Image"];
                            break;
                        case "PDF":

                            item.Image = reportViewerImageList.Images["btnPdf.Image"];
                            break;
                    }
                    //Ẩn tính năng xuất ra word và excel
                    if (extension.Name.ToLower() == "word" || extension.Name.ToLower() == "excel")
                        item.Visible = false;
                }
            }
        }
    }
    ToolStrip ToolStrip
    {
        get { return FindToolStrip<ToolStrip>(reportViewer1); }
    }
}

